extends Node2D

# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var is_touch=false
var draging=false
var radius=120
var initPosition=Vector2()

# Called when the node enters the scene tree for the first time.
func _ready():
	initPosition=position
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

func showJoy():
	show()

func hideJoy():
	hide()

func _input(event):
	if event is InputEventScreenTouch:
		if event.pressed:
			position=event.position
		if !event.pressed:
			position=initPosition
			$Joy.position=Vector2(0,0)
			pass
			
	if event is InputEventScreenDrag:
		var srcPos=Vector2(0,0)
		var viewTransform=$JoyBg.get_viewport_transform().inverse()
		var globalTrasnform=$JoyBg.get_global_transform().inverse()
		var joyPos1= globalTrasnform*(viewTransform*event.position)
		print(joyPos1)
		var joyPos=event.position-position
		print(joyPos)
		print("\n")
		var dxPos=(joyPos-srcPos)
		if dxPos.length()<=radius:
			$Joy.position=joyPos
		else:
			$Joy.position=joyPos.normalized()*radius
		pass
	
func _get_joy_velocity():
	var srcPos=Vector2(0,0)
	var joyPos=$Joy.position
	return (joyPos-srcPos).normalized()

func _on_JoyBg_button_down():
	is_touch=true
	pass # Replace with function body.

func _on_JoyBg_button_up():
	is_touch=false
	pass # Replace with function body.
